package imageProcessing.filter;

import imageProcessing.Duplicator;
import imageProcessing.ImageEvent;
import imageProcessing.ImageListener;

import java.awt.image.renderable.ParameterBlock;
import java.util.ArrayList;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.operator.MedianFilterDescriptor;

public class MedianFilter implements ImageListener {
	
	private ArrayList<ImageListener> _listeners;
	
	private int _filterSize;
	private PlanarImage _image;
	
	public MedianFilter(){
		_listeners = new ArrayList<ImageListener>();
		_image = null;
		_filterSize = 1;
	}

	public void notifyListeners() {
			ImageEvent ie  = new ImageEvent(this, _image);
			
			
		ArrayList<ImageListener> copy = Duplicator.copyListeners(_listeners);
		for (ImageListener listener : copy) {
			listener.imageChanged(ie);
		}
	}
	
    public int getm_filterSize() {
		return _filterSize;
	}

	public void setm_filterSize(int filterSize) {
		this._filterSize = filterSize;
	}
	
	public void addImageListener(ImageListener il){
		_listeners.add(il);
	}
	
	public void removeImageListener(ImageListener il){
		_listeners.remove(il);
	}
	@Override
	public void imageChanged(ImageEvent ie) {		
		PlanarImage img = ie.getImage();
		// TODO: Add Filter-code here
		ParameterBlock pb = new ParameterBlock();
		pb.addSource(img);
		pb.add(MedianFilterDescriptor.MEDIAN_MASK_SQUARE);
		pb.add(_filterSize);
		_image =  JAI.create("MedianFilter", pb);
		notifyListeners();
	}

}
